#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

# Copyright (c) 2005-2014 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys
import libdatadisks
from libdatadisks.util import Util


def usage():
    print('Any USB stick, SD card, removable harddisk or other storage media can be your data disk.')
    print('')

    if len(sys.argv) == 1 or (len(sys.argv) == 2 and sys.argv[1] == "--help"):
        print('Usage:')
        print('   datadisks list       - list data disks')
        print('   datadisks access     - prepare to access this data disk, show where the mount point is')
        print('   datadisks eject      - eject or get this data disk prepared for removal')
        print('   datadisks reformat   - reformat this data disk, erase all data in it')        # auto unmount and remount
        print('')
        return

    if len(sys.argv) == 3 and sys.argv[1] == "list" and sys.argv[2] == "--help":
        print('Usage:')
        print('   datadisks list [-v|--verbose]')
        print('')
        print('Use this command to show all data disks plugged in your system.')
        print('')
        return

    if len(sys.argv) == 3 and sys.argv[1] == "access" and sys.argv[2] == "--help":
        print('Usage:')
        print('   datadisks access [--label LABEL | --device DEVICE] [--read-only]')
        print('')
        print('Description:')
        print('This command mounts your data disk into a predefined directory and displays the path to you. If you have')
        print('multiple data disks on your system simultaneously, you must specify which you want using the --label or --device option.')
        print('If you only need to read and access the data in your data disk, you can specify the --read-only')
        print('option. However, please keep in mind that switching between read-write and read-only modes requires')
        print('ejecting the disk.')
        print('At last, please note that this command will not perform a repeat mount if the data disk is already mounted.')
        print('')
        return

    if len(sys.argv) == 3 and sys.argv[1] == "eject" and sys.argv[2] == "--help":
        print('Usage:')
        print('   datadisks eject [--label LABEL | --device DEVICE]')
        print('')
        return

    if len(sys.argv) == 3 and sys.argv[1] == "reformat" and sys.argv[2] == "--help":
        print('Usage:')
        print('   datadisks reformat [--label LABEL | --device DEVICE] [--set-label LABEL] [--make-compatible-with OS,OS...] [--wipe] [--quiet]')        # auto unmount and remount
        print('')
        return

    assert False


def die(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)


def doShowInfo():
    bVerbose = ("-v" in sys.argv or "--verbose" in sys.argv)

    for obj in libdatadisks.probe_data_disks():
        print("Device: %s" % (obj.dev_path))

        if obj.label is not None:
            print("Label: %s" % (obj.label))

        if bVerbose:
            print("PartitionTableType: %s" % (obj.partition_table_type))
            print("FileSystem: %s" % (obj.fs))
            print("FileSystem-UUID: %s" % (obj.fs_uuid))

        if obj.get_mount_point() is not None:
            total, used = Util.getBlkDevCapacity(obj.get_partition_dev_path())
            print("Size: %s (%s used)" % (Util.formatSize(total), Util.formatSize(used)))
        else:
            sz = Util.getBlkDevSize(obj.get_partition_dev_path())
            print("Size: %s" % (Util.formatSize(sz)))

        if obj.get_mount_point() is not None:
            print("Mountpoint: %s%s" % (obj.get_mount_point(), " (read-only)" if obj.is_read_only() else ""))

        print("")


def doAccess():
    assert False


def doEject():
    assert False


def doFormat():
    assert False


if __name__ == '__main__':
    if len(sys.argv) <= 1:
        usage()
        sys.exit(1)

    if sys.argv[1] == "--help":
        usage()
        sys.exit(0)

    op = sys.argv[1]

    if op == "list":
        if len(sys.argv) >= 3 and sys.argv[2] == "--help":
            usage()
            sys.exit(0)
        doShowInfo()
        sys.exit(0)

    if op == "access":
        if len(sys.argv) >= 3 and sys.argv[2] == "--help":
            usage()
            sys.exit(0)
        doAccess()
        sys.exit(0)

    if op == "eject":
        if len(sys.argv) >= 3 and sys.argv[2] == "--help":
            usage()
            sys.exit(0)
        doEject()
        sys.exit(0)

    if op == "reformat":
        if len(sys.argv) >= 3 and sys.argv[2] == "--help":
            usage()
            sys.exit(0)
        doFormat()
        sys.exit(0)

    die('Invalid operation.')
